/*
 * 扩展一个utils模块
 * */

layui.define(['layer', 'table', 'form', 'laydate'], function (exports) { //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
  var $ = layui.$,
    layer = layui.layer,
    form = layui.form,
    table = layui.table,
    laydate = layui.laydate;
  var utils = {
    // 测试
    hello: function (str) {
      console.log('Hello ' + (str || 'utils'));
    },
    /*时间转换*/
    checkTime: function checkTime(i) {
      if (i < 10) {
        i = '0' + i;
      }
      return i;
    },
    formatTime: function formatTime(date) {
      var getFullYear = this.checkTime(date.getFullYear());
      var getMonth = this.checkTime(date.getMonth() + 1);
      var getDate = this.checkTime(date.getDate());
      var getHours = this.checkTime(date.getHours());
      var getMinutes = this.checkTime(date.getMinutes());
      var getSeconds = this.checkTime(date.getSeconds());
      return getFullYear + "-" + getMonth + "-" + getDate + " " + getHours + ":" + getMinutes + ":" + getSeconds;
    },
    coding: function (btn) {
      var btn_dom = btn;
      if (!btn) {
        btn_dom = '.js-btn-coding';
      }
      $(btn_dom).on('click', function () {
        layer.alert('维护中，需官方授权开放');
        return;
      });
    },
    setTableOption: function (dataName, method) {
      var method = method || 'get';
      table.set({
        method: method,
        request: {
          pageName: 'curpage',
          limitName: 'pagesize'
        },
        response: {
          statusCode: 200,
          msgName: 'message',
          dataName: dataName
        }
      })
    },
    positiveInteger: function () {
      form.verify({
        positiveInteger: [
          /^(0|[1-9]\d*)$/, '请输入正整数'
        ]
      });
    },
    money: function () {
      form.verify({
        momey: [
          /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, '金额只能保留小数点后两位'
        ]
      });
    },
    /*保留两位小数*/
    returnFloat: function returnFloat(value) {
      // var value=Math.round(parseFloat(value)*100)/100;
      var xsd = value.toString().split(".");
      if (xsd.length == 1) {
        value = value.toString() + ".00";
        return value;
      }
      if (xsd.length > 1) {
        if (xsd[1].length < 2) {
          value = value.toString() + "0";
        } else {
          value = xsd[0] + '.' + xsd[1].substr(0, 2);
        }
        return value;
      }
    },
    /*选择时间范围*/
    laydate: function (dom) {
      laydate.render({
        elem: dom, //指定元素
        range: true, //或 range: '~' 来自定义分割字符
        done: function (value, date, endDate) {
          var start_date, end_date;
          var $start = $(dom).siblings('.js-time-start');
          var $end = $(dom).siblings('.js-time-end');
          // console.log(value);
          if (value) {
            start_date = date.year + '-' + utils.checkTime(date.month) + '-' + utils.checkTime(date.date);
            end_date = endDate.year + '-' + utils.checkTime(endDate.month) + '-' + utils.checkTime(endDate.date);
          } else {
            start_date = '';
            end_date = '';
          }
          console.log({
            'start_date': start_date,
            'end_date': end_date
          });
          $start.val(start_date);
          $end.val(end_date);
        }
      });
    },
    showImg: function showImg($this, td_title_index) {
      let idx = td_title_index;
      let type = typeof (idx);
      // let this_id = '#' + $this.attr('id');
      let this_class = '.' + $this.attr('class');
      let td_img_index = $this.closest('td').index();
      let th_img_title = $this.closest('.layui-table-box').find('.layui-table-header thead>tr>th').eq(td_img_index).text();
      let content_img = $this.find('img');
      let th_index_title = '';
      console.log(type)
      if (idx && type === 'number') {
        th_index_title = $this.closest('tr').find('td').eq(idx).text();
        th_img_title = th_index_title + '的' + th_img_title;
      } else if (idx && type === 'string') {
        th_index_title = $this.closest('tr').find('td[data-field="' + idx + '"]').text();
        th_img_title = th_index_title + '的' + th_img_title;
      }
      // console.log(idx, this_str);
      layer.open({
        type: 1,
        shadeClose: true,
        scrollbar: false,
        title: th_img_title || '图像',
        content: $this, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        // area: ['500px', '542px'],
        success: function () {
          $('body').off('click', this_class);
        },
        end: function () {
          $('body').on('click', this_class, function () {
            var $this = $(this);
            console.log(idx);
            utils.showImg($this, idx);
          });
        }
      });
    }
  };
  /* 全局配置 */
  layui.config({
    version: true //一般用于更新模块缓存，默认不开启。设为true即让浏览器不缓存。也可以设为一个固定的值，如：201610
  });
  /* 清空form数据 */
  var $dateRest = $('.layui-form>[type="reset"]');
  $($dateRest).on('click', function () {
    var $start_date = $(this).closest('.layui-form').find('[name="start_time"]');
    var $end_date = $(this).closest('.layui-form').find('[name="end_time"]');
    $start_date.val('');
    $end_date.val('');
  });

  // 输出test接口
  exports('utils', utils);
});